const gulp = require('gulp')
const shell = require('gulp-shell')
const concat = require('gulp-concat')
const merge = require('merge-stream')
const rename = require('gulp-rename')
const streamqueue = require('streamqueue')
const minifyHTML = require('gulp-minify-html')
const run_sequence = require('run-sequence')
const postcss = require('gulp-postcss');
const sourcemaps = require('gulp-sourcemaps');
const ftp = require('gulp-ftp')

const watchui = require('./semantic/tasks/watch')
const buildui = require('./semantic/tasks/build')
gulp.task('ui_watch', watchui)
gulp.task('ui_build', buildui)
gulp.task('import_theme', function () {
    gulp.src('vendor/theme/**/*')
        .pipe(gulp.dest('semantic/src/themes/default'))
})
gulp.task('theme_watch', function () {
    gulp.watch('vendor/theme/**/*', ["import_theme"])
})

const distPath = "./dist"

gulp.task('vendor_css', function () {
    return gulp.src('vendor/vendor_postcss.css')
        .pipe(sourcemaps.init())
        .pipe(postcss([
            require('postcss-import'),
            require('postcss-cssnext'),
            require('postcss-mixins'),
            require('postcss-inline-svg'),
            require('postcss-sorting'),
            // require('stylelint'),
        ]))
        .pipe(sourcemaps.write('.'))
        .pipe(rename('vendor.css'))
        .pipe(gulp.dest('vendor'));
});

gulp.task('vendor_css_rel', function () {
    return gulp.src('vendor/**/*.css')
        .pipe(sourcemaps.init())
        .pipe(postcss([
            require('postcss-import'),
            require('postcss-cssnext'),
            require('postcss-mixins'),
            require('postcss-inline-svg'),
            require('postcss-sorting'),
            require('cssnano'),
            // require('stylelint'),
        ]))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(distPath));
});

gulp.task('vendor', function () {
    return gulp.src([
            'bower_components/lodash/dist/lodash.js',
            'bower_components/jquery-validation/dist/jquery.validate.js',
            "bower_components/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js",
            'bower_components/moment/moment.js',
            'bower_components/moment/locale/zh-cn.js',
            'bower_components/zeroclipboard/dist/zeroclipboard.js',
            'vendor/cook/fine-uploader/jquery.fine-uploader.js',
            'node_modules/pnotify/src/pnotify.js',
            'node_modules/pnotify/src/pnotify.mobile.js',
            'node_modules/pnotify/src/pnotify.buttons.js',
            'node_modules/pnotify/src/pnotify.confirm.js',
            'semantic/dist/semantic.js',
        ])
        .pipe(concat('vendor.js'))
        .pipe(gulp.dest('vendor'))
})

gulp.task('vendor_rel', function () {
    return merge(
        gulp.src([
                'bower_components/lodash/dist/lodash.min.js',
                'bower_components/jquery-validation/dist/jquery.validate.min.js',
                "bower_components/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js",
                'bower_components/moment/min/moment.min.js',
                'bower_components/moment/locale/zh-cn.js',
                'bower_components/zeroclipboard/dist/zeroclipboard.min.js',
                'vendor/cook/fine-uploader/jquery.fine-uploader.min.js',
                'node_modules/pnotify/dist/pnotify.js',
                'node_modules/pnotify/dist/pnotify.mobile.js',
                'node_modules/pnotify/dist/pnotify.buttons.js',
                'node_modules/pnotify/dist/pnotify.confirm.js',
                'semantic/dist/semantic.min.js',
            ])
            .pipe(concat('vendor.js'))
            .pipe(gulp.dest(distPath) + '/vendor')
        ,
        gulp.src('bower_components/jquery/dist/jquery.js')
            .pipe(gulp.dest(distPath + '/bower_components/jquery/dist'))
        ,
        gulp.src('vendor/semantic/dist/*')
            .pipe(gulp.dest(distPath + '/vendor/semantic/dist'))
        ,
        gulp.src('images/**/*')
            .pipe(gulp.dest(distPath + '/images'))
        ,
        gulp.src('*.{htaccess,xml,ico,txt}')
            .pipe(gulp.dest(distPath))
        ,
        gulp.src('./*.html')
            .pipe(minifyHTML({
                conditionals: true,
                spare: true
            }))
            .pipe(gulp.dest(distPath))
    )
})

gulp.task('conf_dev', function () {
    gulp.src('config.dev.js')
        .pipe(rename('config.js'))
        .pipe(gulp.dest('scripts'))
})

gulp.task('conf_rel', function () {
    gulp.src('config.release.js')
        .pipe(rename('config.js'))
        .pipe(gulp.dest('scripts'))
})

gulp.task('ftp', function () {
    gulp.src(distPath + '/**/*.*')
        .pipe(gulp.dest("swallow.web"))
        .pipe(ftp({
            host: '192.168.100.4',
            user: 'root',
            pass: '12345678',
            remotePath: "/"
        }))
})
